package zheng.mockit.samples.http;

import zheng.mockit.AbstractMockitTestCase;
import zheng.mockit.PluginConf;
import zheng.mockit.plugin.http.MockitHttpClient;
import zheng.mockit.plugin.http.MockitHttpServer;
import zheng.mockit.plugin.http.message.MockitHttpRequestMessage;
import zheng.mockit.plugin.http.message.MockitHttpResponseMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zcz on 16-7-29.
 */
public class HttpGetExample2 extends AbstractMockitTestCase {


    @PluginConf("{\"method\":\"get\", \"url\":\"http://127.0.0.1:8080/helloworld\"}")
    private MockitHttpClient mockitHttpClient;
    @PluginConf("{\"port\":8080}")
    private MockitHttpServer mockitHttpServer;


    @Override
    protected void runTC() {
        //1.调起一个请求。
        mockitHttpClient.sendRequest("a=hello&b=world");

        //2.服务端接收请求，并验证。
        MockitHttpRequestMessage httpRequestMessage = new MockitHttpRequestMessage();
        httpRequestMessage.setUri("/helloworld");
        Map<String, String> params = new HashMap<String, String>();
        params.put("a", "hello");
        params.put("b", "world");
        httpRequestMessage.setParams(params);
        mockitHttpServer.assertRequest(httpRequestMessage);

        //3.自定义服务器返回。
        mockitHttpServer.sendResponse("OK");

        //4.验证客户端返回结果。
        MockitHttpResponseMessage responseMessage = new MockitHttpResponseMessage();
        responseMessage.setStatusCode(200);
        responseMessage.setContent("OK");
        mockitHttpClient.assertResponse(responseMessage);
    }
}
